package switchviz.network.netswitch;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.Application;
import switchviz.core.ResourceManager;
import switchviz.core.scenario.ScenarioDataModel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;
import switchviz.recording.RecordDialog;

public class SwitchScenarioConfiguration extends PaddingPanel implements ActionListener, ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4199075503357755127L;
	private JLabeldComponent _cbrecord = null;
	private JMaskedTextField _txttimerintervall = null;
	//private JLabeldComponent _cbsynchron = null;
	private ScenarioDataModel _scenariomodel = null;
	private JButton _buttrecord = null;
	private JPanel _recpanel = null;
	private JLabel _recinfo = null;
	private String[] _selalias = null;
	private String[] _viewalias = null;
	private File _recfile = null;
	
	public SwitchScenarioConfiguration(ScenarioDataModel scenedm)
	{
		_scenariomodel = scenedm;
		initialize();
	}
	
	public File getRecordFile()
	{
		return _recfile;
	}
	
	public String[] getAliasList()
	{
		return _selalias;
	}
	
	public void setSelAliasList(String[] selaliaslist)
	{
		_selalias = selaliaslist;
	}
	
	public void setViewAlias(String[] viewalias)
	{
		_viewalias = viewalias;
	}
	
	private void initialize()
	{
		Dimension labelwidth = new Dimension(250, 20);
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(BorderFactory.createTitledBorder("Switch / SNMP Options"));
		this.setPreferredSize(new Dimension(800, 110));
		
		_cbrecord = new JLabeldComponent("Record Data: ", JLabeldComponent.CHECKBOX, labelwidth);
		//_cbsynchron = new JLabeldComponent("Process Synchron: ", JLabeldComponent.CHECKBOX, labelwidth);
		
		_buttrecord = new JButton("set Record File");
		_buttrecord.addActionListener(this);
		_buttrecord.setPreferredSize(ResourceManager.getInstance().getConfigurationLabelDimension());
		_recinfo = new JLabel("");
		_cbrecord.getCheckBox().setSelected(_scenariomodel.isRecord());
		_cbrecord.getCheckBox().addItemListener(this);
		_txttimerintervall = new JMaskedTextField("SNMP Request Rate (milliseconds): ", JMaskedTextField.INTNUM, labelwidth);
		_txttimerintervall.setMin(10);
		_txttimerintervall.setText(String.valueOf(_scenariomodel.getTimeintervall()));
		//_cbsynchron.getCheckBox().setSelected(_scenariomodel.isProcessSynchron());
		
		_recpanel = new JPanel(new BorderLayout());
		_recpanel.add(_buttrecord, BorderLayout.WEST);
		_recpanel.add(_recinfo, BorderLayout.CENTER);
		_recpanel.setVisible(false);
		_buttrecord.setPreferredSize(labelwidth);
		
		this.add(_txttimerintervall);
		//this.add(_cbsynchron);
		this.add(_cbrecord);
		this.add(_recpanel);
	}
	
	public ScenarioDataModel getScenarioModel()
	{
		_scenariomodel.setRecord(_cbrecord.getCheckBox().isSelected());
		_scenariomodel.setTimeintervall(Integer.valueOf(_txttimerintervall.getText()));
		//_scenariomodel.setProcessSynchron(_cbsynchron.getCheckBox().isSelected());
		return _scenariomodel;
	}

	public void actionPerformed(ActionEvent evt) 
	{
		RecordDialog recdia = new RecordDialog(Application.getInstance().getMainFrame());
		if(_selalias != null)
			recdia.setAliasList(_selalias);
		if(_viewalias != null)
			recdia.setAliasList(_viewalias);
		recdia.setVisible(true);
		_selalias = RecordDialog.getAliasList();
		_recfile = RecordDialog.getRecordFile();
		if(_recfile != null)
		{
			_recinfo.setText("  " + RecordDialog.getRecordFile().getAbsolutePath());
		}
		else
		{
			_recinfo.setText("");
		}
	}


	public void itemStateChanged(ItemEvent evt) 
	{
		if(_cbrecord.getCheckBox().isSelected())
			_recpanel.setVisible(true);//this.add(_recpanel);
		else
			_recpanel.setVisible(false);//this.remove(_recpanel);
		this.validate();
		this.repaint();
	}
}
